package com.marry.blog.controller;

import com.marry.blog.entity.BlogType;
import com.marry.blog.entity.PageBean;
import com.marry.blog.service.BlogTypeService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ml
 * @create 2017-07-31--14:13
 */
@Controller
@RequestMapping(value = "/admin/blogType")
public class BlogTypeController {

    @Autowired
    private BlogTypeService blogTypeService;



    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> listBlogType(String page,String rows){

        Map<String,Object> resultMap=new HashMap<>();


        PageBean<BlogType> pageBean=new PageBean<>(Integer.parseInt(page),Integer.parseInt(rows));

        pageBean=blogTypeService.listByPage(pageBean);

        resultMap.put("rows",pageBean.getResult());

        resultMap.put("total",pageBean.getTotal());

        return resultMap;

    }

    /**
     * 更新或添加
     * @param blogType
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Boolean> saveOrUpdate(BlogType blogType){
        Map<String,Boolean> map=new HashMap<>();
        int resultTotal=0;

        if(blogType.getId() == null){
            resultTotal=blogTypeService.addBlogType(blogType);
        }else{
            resultTotal=blogTypeService.updateBlogType(blogType);
        }

        if(resultTotal>0){

            map.put("success",true);

        }else{
            map.put("success",false);
        }
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Boolean> deleteBlog(String ids){
        Map<String,Boolean> map=new HashMap<>();
        String[] idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            int id=Integer.parseInt(idsStr[i]);

            blogTypeService.deleteBlogType(id);
        }
        map.put("success",true);

        return map;
    }
}
